<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<link rel="STYLESHEET" href="lib.css" type='text/css' />
<link rel="SHORTCUT ICON" href="../icons/pyfav.png" type="image/png" />
<link rel='start' href='../index.html' title='Python documentation Index' />
<link rel="first" href="lib.html" title='Python library Reference' />
<link rel='contents' href='contents.html' title="Contents" />
<link rel='index' href='genindex.html' title='Index' />
<link rel='last' href='about.html' title='About this document...' />
<link rel='help' href='about.html' title='About this document...' />
<link rel="next" href="module-random.html" />
<link rel="prev" href="module-cmath.html" />
<link rel="parent" href="numeric.html" />
<link rel="next" href="decimal-tutorial.html" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name='aesop' content='information' />
<title>6.3 decimal -- Decimal floating point arithmetic</title>
</head>
<body>
<div class="navigation">
<div id='top-navigation-panel' xml:id='top-navigation-panel'>
<table align="center" width="100%" cellpadding="0" cellspacing="2">
<tr>
<td class='online-navigation'><a rel="prev" title="6.2 cmath  "
  href="module-cmath.html"><img src='../icons/previous.png'
  border='0' height='32'  alt='Previous Page' width='32' /></a></td>
<td class='online-navigation'><a rel="parent" title="6. numeric and Mathematical"
  href="numeric.html"><img src='../icons/up.png'
  border='0' height='32'  alt='Up one Level' width='32' /></a></td>
<td class='online-navigation'><a rel="next" title="6.3.1 quick-start Tutorial"
  href="decimal-tutorial.html"><img src='../icons/next.png'
  border='0' height='32'  alt='Next Page' width='32' /></a></td>
<td align="center" width="100%">Python Library Reference</td>
<td class='online-navigation'><a rel="contents" title="Table of Contents"
  href="contents.html"><img src='../icons/contents.png'
  border='0' height='32'  alt='Contents' width='32' /></a></td>
<td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png'
  border='0' height='32'  alt='Module Index' width='32' /></a></td>
<td class='online-navigation'><a rel="index" title="Index"
  href="genindex.html"><img src='../icons/index.png'
  border='0' height='32'  alt='Index' width='32' /></a></td>
</tr></table>
<div class='online-navigation'>
<b class="navlabel">Previous:</b>
<a class="sectref" rel="prev" href="module-cmath.html">6.2 cmath  </a>
<b class="navlabel">Up:</b>
<a class="sectref" rel="parent" href="numeric.html">6. Numeric and Mathematical</a>
<b class="navlabel">Next:</b>
<a class="sectref" rel="next" href="decimal-tutorial.html">6.3.1 Quick-start Tutorial</a>
</div>
<hr /></div>
</div>
<!--End of Navigation Panel-->

<h1><a name="SECTION008300000000000000000">
6.3 <tt class="module">decimal</tt> --
         Decimal floating point arithmetic</a>
</h1>

<p>
<a name="module-decimal"></a>

<p>

<p>

<p>

<span class="versionnote">New in version 2.4.</span>

<p>
The <tt class="module">decimal</tt> module provides support for decimal floating point
arithmetic.  It offers several advantages over the <tt class="class">float()</tt> datatype:

<p>

<ul>
<li>Decimal numbers can be represented exactly.  In contrast, numbers like
<tt class="constant">1.1</tt> do not have an exact representation in binary floating point.
End users typically would not expect <tt class="constant">1.1</tt> to display as
<tt class="constant">1.1000000000000001</tt> as it does with binary floating point.

<p>
</li>
<li>The exactness carries over into arithmetic.  In decimal floating point,
"<tt class="samp">0.1 + 0.1 + 0.1 - 0.3</tt>" is exactly equal to zero.  In binary floating
point, result is <tt class="constant">5.5511151231257827e-017</tt>.  While near to zero, the
differences prevent reliable equality testing and differences can accumulate.
For this reason, decimal would be preferred in accounting applications which
have strict equality invariants.

<p>
</li>
<li>The decimal module incorporates a notion of significant places so that
"<tt class="samp">1.30 + 1.20</tt>" is <tt class="constant">2.50</tt>.  The trailing zero is kept to indicate
significance.  This is the customary presentation for monetary applications. For
multiplication, the ``schoolbook'' approach uses all the figures in the
multiplicands.  For instance, "<tt class="samp">1.3 * 1.2</tt>" gives <tt class="constant">1.56</tt> while
"<tt class="samp">1.30 * 1.20</tt>" gives <tt class="constant">1.5600</tt>.

<p>
</li>
<li>Unlike hardware based binary floating point, the decimal module has a user
settable precision (defaulting to 28 places) which can be as large as needed for
a given problem:

<p>
<div class="verbatim"><pre>
&gt;&gt;&gt; getcontext().prec = 6
&gt;&gt;&gt; Decimal(1) / Decimal(7)
Decimal("0.142857")
&gt;&gt;&gt; getcontext().prec = 28
&gt;&gt;&gt; Decimal(1) / Decimal(7)
Decimal("0.1428571428571428571428571429")
</pre></div>

<p>
</li>
<li>Both binary and decimal floating point are implemented in terms of published
standards.  While the built-in float type exposes only a modest portion of its
capabilities, the decimal module exposes all required parts of the standard.
When needed, the programmer has full control over rounding and signal handling.

<p>
</li>
</ul>

<p>
The module design is centered around three concepts:  the decimal number, the
context for arithmetic, and signals.

<p>
A decimal number is immutable.  It has a sign, coefficient digits, and an
exponent.  To preserve significance, the coefficient digits do not truncate
trailing zeroes.  Decimals also include special values such as
<tt class="constant">Infinity</tt>, <tt class="constant">-Infinity</tt>, and <tt class="constant">NaN</tt>.  The standard
also differentiates <tt class="constant">-0</tt> from <tt class="constant">+0</tt>.

<p>
The context for arithmetic is an environment specifying precision, rounding
rules, limits on exponents, flags indicating the results of operations,
and trap enablers which determine whether signals are treated as
exceptions.  Rounding options include <tt class="constant">ROUND_CEILING</tt>,
<tt class="constant">ROUND_DOWN</tt>, <tt class="constant">ROUND_FLOOR</tt>, <tt class="constant">ROUND_HALF_DOWN</tt>,
<tt class="constant">ROUND_HALF_EVEN</tt>, <tt class="constant">ROUND_HALF_UP</tt>, and <tt class="constant">ROUND_UP</tt>.

<p>
Signals are groups of exceptional conditions arising during the course of
computation.  Depending on the needs of the application, signals may be
ignored, considered as informational, or treated as exceptions. The signals in
the decimal module are: <tt class="constant">Clamped</tt>, <tt class="constant">InvalidOperation</tt>,
<tt class="constant">DivisionByZero</tt>, <tt class="constant">Inexact</tt>, <tt class="constant">Rounded</tt>,
<tt class="constant">Subnormal</tt>, <tt class="constant">Overflow</tt>, and <tt class="constant">Underflow</tt>.

<p>
For each signal there is a flag and a trap enabler.  When a signal is
encountered, its flag is incremented from zero and, then, if the trap enabler
is set to one, an exception is raised.  Flags are sticky, so the user
needs to reset them before monitoring a calculation.

<p>
<div class="seealso">
  <p class="heading">See Also:</p>

  <div class="seetext"><p>IBM's General Decimal Arithmetic Specification,
           <em class="citetitle"><a
 href="http://www2.hursley.ibm.com/decimal/decarith.html"
 title="The General Decimal Arithmetic Specification"
 >The General Decimal Arithmetic Specification</a></em>.</p></div>

<p>
<div class="seetext"><p>IEEE standard 854-1987,
           <em class="citetitle"><a
 href="http://www.cs.berkeley.edu/~ejr/projects/754/private/drafts/854-1987/dir.html"
 title="Unofficial IEEE 854 Text"
 >Unofficial IEEE 854 Text</a></em>.</p></div> 
</div>

<p>

<p><br /></p><hr class='online-navigation' />
<div class='online-navigation'>
<!--Table of Child-Links-->
<a name="CHILD_LINKS"><strong>Subsections</strong></a>

<ul class="ChildLinks">
<li><a href="decimal-tutorial.html">6.3.1 Quick-start Tutorial</a>
<li><a href="node126.html">6.3.2 Decimal objects</a>
<li><a href="decimal-decimal.html">6.3.3 Context objects</a>
<li><a href="decimal-signals.html">6.3.4 Signals</a>
<li><a href="decimal-notes.html">6.3.5 Floating Point Notes</a>
<ul>
<li><a href="node130.html">6.3.5.1 Mitigating round-off error with increased precision</a>
<li><a href="node131.html">6.3.5.2 Special values</a>
</ul>
<li><a href="decimal-threads.html">6.3.6 Working with threads</a>
<li><a href="decimal-recipes.html">6.3.7 Recipes</a>
<li><a href="decimal-faq.html">6.3.8 Decimal FAQ</a>
</ul>
<!--End of Table of Child-Links-->
</div>

<div class="navigation">
<div class='online-navigation'>
<p></p><hr />
<table align="center" width="100%" cellpadding="0" cellspacing="2">
<tr>
<td class='online-navigation'><a rel="prev" title="6.2 cmath  "
  href="module-cmath.html"><img src='../icons/previous.png'
  border='0' height='32'  alt='Previous Page' width='32' /></a></td>
<td class='online-navigation'><a rel="parent" title="6. numeric and Mathematical"
  href="numeric.html"><img src='../icons/up.png'
  border='0' height='32'  alt='Up one Level' width='32' /></a></td>
<td class='online-navigation'><a rel="next" title="6.3.1 quick-start Tutorial"
  href="decimal-tutorial.html"><img src='../icons/next.png'
  border='0' height='32'  alt='Next Page' width='32' /></a></td>
<td align="center" width="100%">Python Library Reference</td>
<td class='online-navigation'><a rel="contents" title="Table of Contents"
  href="contents.html"><img src='../icons/contents.png'
  border='0' height='32'  alt='Contents' width='32' /></a></td>
<td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png'
  border='0' height='32'  alt='Module Index' width='32' /></a></td>
<td class='online-navigation'><a rel="index" title="Index"
  href="genindex.html"><img src='../icons/index.png'
  border='0' height='32'  alt='Index' width='32' /></a></td>
</tr></table>
<div class='online-navigation'>
<b class="navlabel">Previous:</b>
<a class="sectref" rel="prev" href="module-cmath.html">6.2 cmath  </a>
<b class="navlabel">Up:</b>
<a class="sectref" rel="parent" href="numeric.html">6. Numeric and Mathematical</a>
<b class="navlabel">Next:</b>
<a class="sectref" rel="next" href="decimal-tutorial.html">6.3.1 Quick-start Tutorial</a>
</div>
</div>
<hr />
<span class="release-info">Release 2.5.1, documentation updated on 18th April, 2007.</span>
</div>
<!--End of Navigation Panel-->
<address>
See <i><a href="about.html">About this document...</a></i> for information on suggesting changes.
</address>
</body>
</html>
